package com.yangle.frame.common.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yangle.frame.admin.entity.BaseEntity;
import com.yangle.frame.admin.entity.FrameMenu;
import com.yangle.frame.admin.entity.ResultStatus;
import com.yangle.frame.common.service.BaseService;
import com.yangle.frame.common.utils.Request2Object;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.UUID;


/**
 * Created by yangle on 2018/6/3.
 */
public class BaseController<T> {

    public void list(HttpServletRequest request, T entity, BaseService<T> service){
        try {
            Integer pageCurrent=request.getParameter("pageCurrent")!=null? Integer.parseInt(request.getParameter("pageCurrent")) :1;
            Integer pageSize=request.getParameter("pageSize")!=null? Integer.parseInt(request.getParameter("pageSize")) :10;
            PageHelper.startPage(pageCurrent,pageSize);
            List<T> entitys = service.getAll(entity);
            PageInfo<T> pageInfo=new PageInfo<T>(entitys);
            request.setAttribute("entitys",entitys);
            request.setAttribute("total",pageInfo.getTotal());
            request.setAttribute("pageCurrent",pageCurrent);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("entity",entity);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
    public void edit(HttpServletRequest request,BaseService<T> service){
        String id=request.getParameter("id");
        if(id!=null){
            T entity= service.select(id);
            request.setAttribute("entity",entity);
        }else{
            request.setAttribute("entity",new BaseEntity());
        }
    }
    public void create(HttpServletRequest request,T entity, BaseService<T> service){
        ResultStatus result=new ResultStatus(true,"录入成功");
        service.create(entity);
        request.setAttribute("result",result);
    }
    public void update(HttpServletRequest request,T entity, BaseService<T> service){
        service.update(entity);
        ResultStatus result=new ResultStatus(true,"更新成功");
        request.setAttribute("result",result);
    }

    public void delete(HttpServletRequest request,BaseService<T> service){
        service.delete(request.getParameter("ids"));
        ResultStatus resultStatus=new ResultStatus(true,"删除成功");
        request.setAttribute("result",resultStatus);
    }

    public void view(HttpServletRequest request,BaseService<T> service){
        String id=request.getParameter("id");
        T entity= service.select(id);
        request.setAttribute("entity",entity);
    }
}
